import json
import requests
from PySide2 import QtCore, QtWidgets
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QLabel, QLineEdit


# 模板位置改变窗口
from robotController import share
from robotController.binding import template_bind


class template_move_window(QWidget):
    def __init__(self,id ,parent=None):
        super(template_move_window, self).__init__(parent)
        self.setStyleSheet("""
            background-color: rgb(255, 255, 255);
        """)
        # 设置窗口大小
        self.setFixedSize(200, 100);
        # 设置题头
        self.setWindowTitle("更改模板位置")
        # 隐藏最大化最小化
        self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint | QtCore.Qt.WindowStaysOnTopHint)

        # 添加框组件绑定（任务名，备注） 最大总体布局为纵向布局
        vLayout = QVBoxLayout()
        # 创建填写布局
        # 新建按钮组
        buttons = self.move_buttons(id)
        param_body = self.param_body()
        vLayout.addWidget(param_body)
        vLayout.addWidget(buttons)
        self.setLayout(vLayout)

        # 新建保存按钮组
    def move_buttons(self,id):
        widget = QWidget()
        # 确定
        confirmBtn = QPushButton('确定',self)
        confirmBtn.setStyleSheet(''' text-align : center;
                                        background-color : #2B77CF;
                                        height : 25px;
                                        border-style: outset;
                                        padding:0 15px;
                                        font : 13px; ''')

        confirmBtn.clicked.connect(lambda : self.confirmClick(id))

        # 取消
        cancelBtn = QPushButton('取消')
        cancelBtn.setStyleSheet(''' text-align : center;
                                        background-color : #D20A0A;
                                        height : 25px;
                                        border-style: outset;
                                        padding:0 15px;
                                        font : 13px; ''')

        cancelBtn.clicked.connect(lambda : self.close())

        # 确认删除按钮布局
        hLayout = QHBoxLayout()
        hLayout.setObjectName("buttonLayout")
        hLayout.addStretch();
        hLayout.addWidget(confirmBtn)
        hLayout.addWidget(cancelBtn)
        hLayout.setContentsMargins(5,2,5,2)
        widget.setLayout(hLayout)
        return widget

    # 确定修改位置
    def confirmClick(self,id):
        # 查询分组及单位
        from robotController.binding.home_bind import home_bind
        ug = home_bind().get_ug_comboBox_fun()
        url = share.RURL.backendUrl + "/template/alterRow/" + str(id) + "/" + self.row_num_LineEdit.text() + "/" + ug["group_value"] + "/" + ug["unit_value"]
        requests.encoding = 'utf-8'
        result = requests.request(method="PUT", url=url, headers=share.token.headers)
        dataMap = json.loads(result.text)

        try:
            if dataMap["flag"] == True:
                QtWidgets.QMessageBox.about(self, "信息提示框", dataMap["message"])
                self.close()
                template_bind.bind().findAll()
        except:
            QtWidgets.QMessageBox.about(self, "信息提示框", "位置修改失败")

    # 位置组件
    def param_body(self):
        widget = QWidget()
        str1 = QLabel("将模板调整至:")
        # 行号
        self.row_num_LineEdit = QLineEdit(self)
        str2 = QLabel("行")
        # 进行任务名及填写框布局
        hLayout1 = QHBoxLayout()
        hLayout1.addWidget(str1)
        hLayout1.addWidget(self.row_num_LineEdit)
        hLayout1.addWidget(str2)

        widget.setLayout(hLayout1)
        return widget